<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2022/1/27
 * Time: 17:33
 */

namespace app\controller;


use app\BaseController;
use app\model\Users;
use think\facade\Db;
use app\model\Test as testModel;
use think\facade\Request;

class Test
{
    public function index(){
        $res = Db::table("cmf_users")->select();
        $res2 = Db::connect("demo")->table("cmf_users")->select();
        dump($res2);
    }

    public function userModel(){
        $res = Users::select();

        //获取执行的原生sql语句
//        Users::getLastSql();
        //单条数据查询  find 开头单条数据查询
        //返回  一个错误   表数据异常
//        $res = Users::where('id',1)->findOrFail();
        //返回一个空数据组
//        $res = Users::where('id',000)->findOrEmpty();

        //数据集查询
        //返回一个异常  数据不存在
//        $res = Users::where('id',0)->selectOrFail();
//        $res = Users::where('id',1)->value("user_nicename");

        //单个值查询
//        $res = Users::where('id',1)->value("user_nicename");
//        $res = Users::where('id',1)->column("id,user_nicename",'id');

//        Db::name("users")->chunk(3,function ($data){
//            foreach ($data as $val){
//                dump($val);
//
//            }
//            echo 2222222;
//        });
        $user = Db::name("users");
        $res = $user->select();
        $res = $user->where("id",1)->select();
//        foreach ($res as $value ){
//            dump($value->toArray());
//        }
        dump($res);
    }
    public function showYield(){
        $data =$this->createRange2(10);
        foreach($data as $value){
            sleep(1);//这里停顿1秒，我们后续有用
            echo $value.PHP_EOL."<br>";
        }
    }
    function createRange($number){
        $data = [];
        for($i=0;$i<$number;$i++){
            $data[] = time();
        }
        return $data;
    }
    function createRange2($number){
        for($i=0;$i<$number;$i++){
            yield time();
        }
    }

    public function testModel(){
//        $data = [
//            "id"        =>      1,
//            "name"      =>      "sanguo"
//        ];
//        $res = testModel::where('id',1)->update($data);
//        $res = testModel::update($data);
        //exp   执行  sql的函数
//        $res = testModel::where('id',1)->exp("name","UPPER(name)")->update();
//        $res = testModel::where('id',1)->dec('desc',3)->update();
//        $res = Db::name("test")->where('id',1)
//                ->update([
//                    "name"      =>      Db::raw('UPPER("name")'),
////                    "desc"      =>      Db::raw("desc + 1"),
//                ]);
//        $data=[
//          ["name"        =>          "fufu"  ],
//          ["name"        =>          "qingtian"  ],
//          ["name"        =>          "laoiz"  ],
//          ["name"        =>          "zhangyi"  ],
//        ];
//        $res =  testModel::where('id',"<>",1)->exp("addtime","NOW()")->update();
//        $res =  testModel::where('name',"like","l%")->exp("addtime","NOW()")->select();
//        $res =  Db::name("test")->field("name")->group('id')->select();


        //withAttr  对字段单独做处理  进行返回
//        $res = Db::name("test")->withAttr("name",function ($value,$data){
//            return strtoupper($value);
//        })->select();
////        $res->pop();
//
//
//        $res = testModel::find(1);
////        $res -> name =  "lalala";
////        $res->save();
////        echo Db::getLastSql();
//        dump($res->toArray());
////            $test = new testModel();
//            app("Request");
//          dump( app("request"));
       $res = Request::param('age');
//        dump($res);
//        dump(Request::header());
//        dump(Request::header('host'));
//        dump(response()->header(['aa' =>  '22']));
//        dump(response()->data([222222])->allowCache(false));
        return redirect(url('Index/hello'))->with('name',"Mr.SU")->remember();
    }


}